package com.hou.controller;

import com.hou.mapper.UserMapper;
import com.hou.pojo.Result;
import com.hou.pojo.User;
import com.hou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.ConsoleHandler;

/**
 * @author shkstart
 * @create 2021-09-29-9:01
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

    // 用户登录
    @GetMapping("login")
    public Result userLogin(String userName, String password){
        Result result = userService.nameMatchPassword(userName, password);
        return result;
    }

    @GetMapping("status")
    // 查询用户等级
    public String userStatus(Integer id){
        if (userService.userStatus(id).equals("root")){
            return "root";
        } else {
            return "user";
        }
    }

    @PostMapping("add")
    // 增加用户
    public Boolean addUser(@RequestBody User user){
        if (userService.addUser(user)){
            return true;
        } else {
            return false;
        }
    }

    @GetMapping("delete")
    // 删除用户
    public Boolean deleteUser (Integer id){
        System.out.println(id);
        if (userService.deleteUser(id)){
            return true;
        } else {
            return false;
        }
    }

    @PostMapping("update")
    // 更改用户
    public Boolean updateUser (@RequestBody User user){
        if (userService.updateUser(user)){
            return true;
        } else {
            return false;
        }
    }

    @GetMapping("getAll")
    // 查询所有用户
    public List getAllUser(){
        List list = userService.selectAllUser();
        return list;
    }

    // 用户信息回显
    @GetMapping("getUser")
    public Map user(Integer id){
        HashMap<String, Object> map = new HashMap<>();
        try {
            User nowUser = userService.getNowUser(id);
            map.put("result",true);
            map.put("user",nowUser);
        } catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
}
